package cn.formaggie.controller.manage;

import cn.formaggie.annotation.CheckTwiceSubmit;
import cn.formaggie.controller.BaseController;
import cn.formaggie.entity.model.PageBean;
import cn.formaggie.entity.model.order.Order;
import cn.formaggie.entity.model.order.OrderItem;
import cn.formaggie.service.face.OrderItemService;
import cn.formaggie.service.face.OrderService;
import cn.formaggie.service.face.SampleService;
import cn.formaggie.service.face.StoreItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/back/orderitem")
public class OrderItemManageController extends BaseController {

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private SampleService sampleService;

    @Autowired
    private StoreItemService storeItemService;

    @Autowired
    private OrderService orderService;

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/list")
    public ModelAndView list(@ModelAttribute(STATE_MSG)String stateMsg){
        ModelAndView mav=new ModelAndView();
        mav.addObject(STATE_MSG,stateMsg);
        mav.addObject("orders",orderService.getAllOrders());
        mav.addObject("samples",sampleService.getAllSamples());
        mav.addObject("pageName","订单详情管理");
        mav.addObject("mainPage",BACK_PAGE_PATH+"/manager/order/item/list.jsp");
        mav.setViewName("/backpage/backTemp");
        return mav;
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/load")
    @ResponseBody
    public Map load(@RequestParam(value = "s_orderCode",required = false)String orderCode,
                    @RequestParam(value = "s_sampleCode",required = false)String sampleCode,
                    @RequestParam(value = "s_sampleName",required = false)String sampleName,
                    @RequestParam(value = "s_predate",required = false)Date predate,
                    @RequestParam(value = "s_enddate",required = false)Date enddate,
                    @RequestParam(value = "page") Integer page,
                    @RequestParam(value = "limit") Integer limit){
        PageBean pageBean=new PageBean(page,limit);
        Map<String,Object> sqlMap=new HashMap<>();
        sqlMap.put("orderCode",orderCode);
        sqlMap.put("sampleCode",sampleCode);
        sqlMap.put("sampleName",sampleName);
        sqlMap.put("predate",predate);
        sqlMap.put("enddate",enddate);
        sqlMap.put("pageBean",pageBean);

        List<OrderItem> data=orderItemService.findOrderItems(sqlMap);
        Integer count=orderItemService.getOrderItemsCount(sqlMap);

        Map<String,Object> viewMap=new HashMap<>();
        viewMap.put("data",data);
        viewMap.put("count",count);
        viewMap.put("code",0);
        viewMap.put("msg","");
        return viewMap;
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/modifyfinish")
    @ResponseBody
    public boolean modifyFinish(Integer id,Boolean state){
        OrderItem item=new OrderItem();
        item.setId(id);
        if(!state){
            item.setIsout(false);
            item.setIspay(false);
        }
        item.setIsfinish(state);
        return orderItemService.modifyState(item);
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/modifyout")
    @ResponseBody
    public boolean modifyOut(Integer id,Boolean state){
        OrderItem item=new OrderItem();
        item.setId(id);
        if(state){
            item.setIsfinish(true);
        }else {
            item.setIspay(false);
        }
        item.setIsout(state);
        return orderItemService.modifyState(item);
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/modifypay")
    @ResponseBody
    public boolean modifyPay(Integer id,Boolean state){
        OrderItem item=new OrderItem();
        item.setId(id);
        if(state){
            item.setIsout(true);
            item.setIsfinish(true);
        }
        item.setIspay(state);
        return orderItemService.modifyState(item);
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/add")
    @CheckTwiceSubmit
    public String addOrderItem(OrderItem orderItem, RedirectAttributes model){
        Boolean state=orderItemService.addOrderItem(orderItem);
        model.addFlashAttribute(STATE_MSG,state?"添加成功":"添加失败");
        return "redirect:/back/orderitem/list";
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/modifyInfo")
    @CheckTwiceSubmit
    public String modifyOrderItemInfo(OrderItem orderItem, RedirectAttributes model){
        Boolean state=orderItemService.modifyOrderItemInfo(orderItem);
        model.addFlashAttribute(STATE_MSG,state?"修改成功":"修改失败");
        return "redirect:/back/orderitem/list";
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping(value = "/delete" ,method = RequestMethod.POST)
    @ResponseBody
    public boolean deleteOrderItem(@RequestParam("id") Integer id){
        return orderItemService.deleteOrderItem(id);
    }

}
